package com.linux.huhx.stringTest;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by huhx on 2017-06-14.
 */
public class FilePathTest {
    @Test
    public void filePath_1() {
        File file = new File("src/main/java/com/file/huhx.txt"); // 相对路径
        System.out.println(file.exists()); // true

        file = new File("com/file/huhx.txt"); // 相对路径
        System.out.println(file.exists()); // false

        InputStream stream = FilePathTest.class.getResourceAsStream("/com/file/huhx.txt"); // I love you, L。
        try {
            String result = IOUtils.toString(stream, "UTF-8");
            System.out.println(result); // I love you.
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
